﻿using UnityEngine;
using System.Collections;
using NetWork;
using AssetLoad;
using ProtoBuf.Message;
using Dict.Blo;

namespace Room
{
    public class RoomField : RoomBase
    {
        public const string bloodBarPrefab= @"UIcomponent/BloodBar";
        public static string particleDieAb = @"particles/01_general/monster_die";
        public static string particleAttAb = @"particles/UI_DXP/changjingzhandou_jian";

        public static string particleDefAb = @"particles/UI_DXP/changjingzhandou_dun";
        public static string particleLitAb = @"particles/UI_DXP/changjingzhandou_guang";
        public static string particleRebornAb = @"particles/UI_DXP/changjingzhandou_guang_1";
        public static string particleNoEnemy = @"particles/03_buff/Buff_fuhuowudi";
        public static string particleRecoverHp = @"particles/UI_TJ/bloodbag";
        private string scene_map;


        public override SceneType GetSceneType()
        {
            return SceneType.FIELD;
        }

        public override ServerType GetServerType()
        {
            return ServerType.field;
        }

        public override void Init()
        {
            base.Init();
            particleAttAb = "particles/" + DictParticleBlo.GetCommonParticle("9").ResourcePath;
            particleDefAb = "particles/" + DictParticleBlo.GetCommonParticle("10").ResourcePath;
            particleLitAb = "particles/" + DictParticleBlo.GetCommonParticle("11").ResourcePath;
            particleRebornAb = "particles/" + DictParticleBlo.GetCommonParticle("8").ResourcePath;
            particleNoEnemy = "particles/" + DictParticleBlo.GetCommonParticle("14").ResourcePath;
            particleRecoverHp = "particles/" + DictParticleBlo.GetCommonParticle("RecoverHp").ResourcePath;
            
            inited = true;
        }

        public override void InitRoomNet()
        {
            netManager = new RoomNetField();
            netManager.Init(this);
        }

        protected override void OnEnter(params object[] param)
        {
            base.OnEnter(param);
            

            if (!inited)
            {
                Init();
            }
            
            StartCoroutine(_WaitForLoadComplete());
            ManagerController.Instance.cameraTool.SwitchSceneCamera();
            Foreground();
            
            

            StaticData.fieldData.playerInField = true;
          
        }
  
        private IEnumerator _WaitForLoadComplete()
        {
            scene_map = Dict.Blo.DictFieldBlo.GetOne(StaticData.fieldId).FieldMap;

            yield return StartCoroutine(AssetBundleManager.Instance.LoadSceneAsync(SceneName.sceneFolder
                 + scene_map, scene_map));
            yield return StartCoroutine(_RegistParticle());

            OnLoadComplete(); 
        }
        
        IEnumerator _RegistParticle()
        { 
            AssetLoad.ResourceManager.Instance.LoadResource(bloodBarPrefab);//血条
            GameObjectPoolManager.Instance.Regist(bloodBarPrefab);
            
            yield return RegistAssets(particleLitAb);
            yield return RegistAssets(particleDefAb);
            yield return RegistAssets(particleDieAb);
            yield return RegistAssets(particleAttAb);
            yield return RegistAssets(particleRebornAb);
            yield return RegistAssets(particleNoEnemy);
            yield return RegistAssets(particleRecoverHp);
        }

        IEnumerator RegistAssets(string str)
        {
            AssetBundleManager.Instance.LoadAssetBundle(str);
            while (!AssetBundleManager.Instance.IsLoadedAssetBundle(str))
            {
                yield return null;
            }
            GameObjectPoolManager.Instance.Regist(str, CommonMethod.GetAssetNameFromAssetBundleName(str));
        }
       

        public void UnRegistParticle()
        {
            GameObjectPoolManager.Instance.UnRegist(particleAttAb, CommonMethod.GetAssetNameFromAssetBundleName(particleAttAb));
            GameObjectPoolManager.Instance.UnRegist(particleDefAb, CommonMethod.GetAssetNameFromAssetBundleName(particleDefAb));
            GameObjectPoolManager.Instance.UnRegist(particleLitAb, CommonMethod.GetAssetNameFromAssetBundleName(particleLitAb));
            GameObjectPoolManager.Instance.UnRegist(particleDieAb, CommonMethod.GetAssetNameFromAssetBundleName(particleDieAb));
            GameObjectPoolManager.Instance.UnRegist(particleRebornAb, CommonMethod.GetAssetNameFromAssetBundleName(particleRebornAb));
            GameObjectPoolManager.Instance.UnRegist(particleNoEnemy, CommonMethod.GetAssetNameFromAssetBundleName(particleNoEnemy));
            GameObjectPoolManager.Instance.UnRegist(particleRecoverHp, CommonMethod.GetAssetNameFromAssetBundleName(particleRecoverHp));

            GameObjectPoolManager.Instance.UnRegist(bloodBarPrefab);
        }

        protected void OnLoadComplete()
        {
            LoadSafeArea();
            netManager.Enter();
            smallMap.ShowMiniMap();
            smallMap.SetData(Dict.Blo.TableSceneBlo.GetSceneById(scene_map));

            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle, 0, 0), cameraFOV);
            StaticData.cameraRotate = Vector3.zero;

            if(StaticData.netBreak)//如果是断网的时候
            {
                ManagerController.Instance._CompleteTownToField();
            }

            UI.UIFieldRunMap.fieldId = StaticData.fieldId;

            CameraTool.Instance.ResetSceneBloomEffect();
        }
        
        protected override void OnLeave()
        {
            base.OnLeave();

            StaticData.fieldData.playerInField = false;

            netManager.Leave();
            
            smallMap.HideMiniMap();
            smallMap.Unload();

            charactorManager.ClearAll();
            UnRegistParticle();

            UI.UIFieldRunMap.RefreshBossTime(0f);

            StaticData.ClearFieldCache();
        }
        

        
        
        /// <summary>
        /// 加载安全区域
        /// </summary>
        void LoadSafeArea()
        {
            charactorManager.CreateSafeArea(StaticData.fieldId);
        }

        void UnloadSafeArea()
        {
            charactorManager.DestroyCharactor(StaticData.fieldId);
        }
        public override void OnMainCharactorLoaded(Transform mc)
        {
            base.OnMainCharactorLoaded(mc);
            viewController.SetPlayer(mc);

            ManagerController.Instance._CompleteTownToField();



            NetAPI.RegistSocketHandler(OpDefine.SCSceneChatCache, (pack) =>
            {
                SCSceneChatCache msg = pack as SCSceneChatCache;
                Chat.ChatManager.Instance.ReceiveChat(msg.chatList);
            });

            NetAPI.SendSocket(OpDefine.CSSceneChatCache, new CSSceneChatCache());
        }

        protected override void Tick()
        {
            base.Tick();

            netManager.Tick();
            smallMap.Tick();
            viewController.Tick();
            charactorManager.Tick();
            sceneCamera.Set(cameraDelta, new Vector3(cameraAngle, 00, 0), cameraFOV);
        }
        protected override void OnForeground()
        {
            //gameObject.SetActive(true);
            netManager.Resume();
            viewController.Resume();
            uiManager.gameObject.SetActive(true);
            EasyTouchTool.Instance.ShowJoystick();
        }

        protected override void OnBackground()
        {
            //gameObject.SetActive(false);
            netManager.Pause();
            viewController.Pause();
            uiManager.gameObject.SetActive(false);
            EasyTouchTool.Instance.Hide();
        }

        public override void PlayerSay(string playerId, string word)
        {
            var charactor = charactorManager.GetCharactor(playerId);
            if (charactor != null)
            {
                charactor.ShowBubble(word);
            }
        }
    }
}